#! /usr/bin/perl -w
#------------------------------------------------
# Author:    kasula
# Created:   Mon Sep 12 22:29:37 PDT 2011
# File Name: Account
# USAGE: 
# 
# 
# 
#------------------------------------------------
use Getopt::Long ;

($DD, $MM, $YYYY) = (localtime) [3,4,5];
$YYYY += 1900;
$MM += 1;

$Date   = sprintf "%04d-%02d-%02d", $YYYY, $MM, $DD;
$DataDir= "/home/$ENV{USER}/.AccBooks";
$From   = "Wallet";
$To     = "Spent";

$Res = GetOptions (
        "dir=s" => \$DataDir,
        "date=s" => \$Date, 
        'reason|comment=s' => \$Reason,
        "from=s" => \$From,
        "to=s" => \$To,
        "amount=i" => \$Amount,
);
die "Please check if all variables are defined properly\n" if ($Res != 1 || !$DataDir || !$Date || !$Reason || !$From || !$To || !$Amount);
mkdir $DataDir ;

$From = lc $From;
$To = lc $To;
foreach $WorkBook ( "$DataDir/$From.csv", "$DataDir/$To.csv" ) {
        if (! -e $WorkBook) {
                print "Creating an empty WorkBook : $WorkBook\n";
                open  FH, ">$WorkBook" or die "Creating WorkBook : $WorkBook failed\n";
                print FH "Date,Reason,Debit,Credit,Balance\n";
                print "Wish to add Opening Balance...? : ";
                chomp ($Bal = <STDIN>);
                print FH "$Date,OpeningBalance,-,-,$Bal\n";
                close FH;
        }
}
open  FH, "+<$DataDir/$From.csv" or die "Opening WorkBook : $From failed\n";
$LastLine = $_ while <FH>;
$Bal = (split /,/, $LastLine)[4];
printf FH "$Date,$Reason,$Amount,-,%d\n", $Bal-$Amount;
close FH;

open  FH, "+<$DataDir/$To.csv"   or die "Opening WorkBook : $To failed\n";
$LastLine = $_ while <FH>;
$Bal = (split /,/, $LastLine)[4];
printf FH "$Date,$Reason,-,$Amount,%d\n", $Bal+$Amount;
close FH;

printf "Modified WorkBooks :\n\t$DataDir/$From.csv\n\t$DataDir/$To.csv\n";
